package com.guochuang.yunwang.promanager.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guochuang.yunwang.promanager.dao.RoleMapper;
import com.guochuang.yunwang.promanager.domain.vo.RoleVO;
import com.guochuang.yunwang.promanager.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guochuang.yunwang.promanager.domain.Role;
//import javax.management.relation.Role;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.getRoleById(id);
    }

    @Override
    public List<Role> getByPage(Integer num, Integer size, String rname) {
        if (num != null && size != null) {
            Page<Object> page = PageHelper.startPage(num, size);
            List<Role> entities = roleMapper.getRoleByName(rname);
            PageInfo<Role> info = new PageInfo<>(entities);
            System.out.println(page);
            System.out.println(page.getResult());
            System.out.println(info);

            return info.getList();
        }
//        Page<Object> page = PageHelper.startPage(num, size);
        List<Role> entities = roleMapper.getRoleByName(rname);
//        PageInfo<CategoryEntity> info = new PageInfo<>(entities);
//        return info.getList();
        return entities;
    }

    @Override
    public Integer addRole(Role role) {
        return roleMapper.addNewRole(role);
    }



    @Override
    public Integer delRoleById(Integer rid) {
        return roleMapper.deleteRoleById(rid);
    }

    @Override
    public Integer delRoleByIds(List<Integer> rids) {
        return roleMapper.deleteRoleByIds(rids);
    }

    @Override
    public Integer updateRole(Role role) {
        return roleMapper.updateRoleById(role);
    }

    @Override
    public PageInfo getRoleByPage(Integer page, Integer offset) {
        PageHelper.startPage(page,offset);

        List<Role> data = roleMapper.findAllRoles();

        PageInfo<Role> rec = new PageInfo<>(data);

        return rec;
    }

    @Override
    public PageInfo findroleByrname(RoleVO rolevo) {
        PageHelper.startPage(rolevo.getPage(),rolevo.getOffset());

        List<Role> roles = roleMapper.getRoleByName(rolevo.getRname());

        PageInfo<Role> pageInfo = new PageInfo<>(roles);

        return pageInfo;
    }

    @Override
    public Integer updateRoleAuths(Integer rid, List<Integer> aids) {

        // 首先删去该角色的所有角色
        Integer delauths = roleMapper.delRoleAuthByRid(rid);

        if(aids.size()==0){
            return delauths;
        }

        // 将新的角色关系插入
        Integer nums = roleMapper.insertRoleAuths(rid,aids);

        return nums;

    }

    @Override
    public Integer addRoleAndAuths(Role role, List<Integer> aids) {
        Integer addrole = roleMapper.addNewRole(role);

        Integer rid = roleMapper.selRidbyRname(role);

        Integer nums = roleMapper.insertRoleAuths(rid,aids);

        return nums;
    }

    @Override
    public List<Role> getAllRoles(){
        return roleMapper.getAllRoles();
    }

    @Override
    public List<Integer> getRoleAuthByRid(Integer rid) {
        return roleMapper.getRoleAuthByRid(rid);
    }
}
